﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodeMan.Application.Contracts.ClassGens;
using CodeMan.Application.Contracts;
using CodeMan.Domain.ClassGens;

using Microsoft.Extensions.Logging;
using CodeManDesktopBlazor.BlazorComponents.Models;
using Microsoft.Extensions.Configuration;
using CodeManDesktopBlazor.BlazorComponents.Data;
using System.Diagnostics.CodeAnalysis;
using CodeMan.ClassGens;
using Microsoft.AspNetCore.Components.Web;

namespace CodeManDesktopBlazor.BlazorComponents.Pages
{

    public partial class AppConfigSet : ComponentBase
    {
        #region Service
        private IOtherDBAppService OtherDBAppService { get; set; }

        #endregion
        #region Inject

        [Inject]
        ILogger<AppConfigSet> Logger { get; set; }

        [Inject]
        NavigationManager NavigationManager { get; set; }
        [Inject]
        private IStringLocalizer<AppConfigSet> Localizer { get; set; }
        [Inject]
        private IStringLocalizer<Connectionstrings> LocalizerConnect { get; set; }

        [Inject]
        private IConfiguration Configuration { get; set; }
        /// <summary>
        ///
        /// </summary>
        [Inject]
        private IJSRuntime JSRuntime { get; set; }
        [Inject]
        private ToastService ToastService { get; set; }

        #endregion
        #region Params

        private AppSetting appSetting { get; set; }
        private IConfigurationSection getSection { get; set; }
        /// <summary>
        /// 获得/设置 CodeGen 高度
        /// </summary>
        [Parameter] public int? Height { get; set; }
        [Parameter] public int? Width { get; set; }
        /// <summary>
        /// 获得 wrapper 样式表集合
        /// </summary>
        protected string? FixedHeaderStyleName => CssBuilder.Default()
            .AddClass($"height: {Height}px;", Height.HasValue)
            .AddClass($"min-width: {Width}px;", Width.HasValue)
            .Build();

        [NotNull]
        private Toast? Toast { get; set; }
        #endregion

        #region OnInitialized

        /// <summary>
        /// 
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override async Task OnInitializedAsync()
        {
            //init service
            OtherDBAppService = GData.currMainWindow.GetOtherDBAppService();

            //int params
            Height = GData.mainjs.heightInt - 40;
            Width = GData.mainjs.widthInt - 10;

            getSection = Configuration.GetSection("Connectionstrings");
            var attstr = getSection.Get<Connectionstrings>();
            appSetting = new AppSetting() { connectionstrings = attstr };

            await base.OnInitializedAsync();
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);

            if (firstRender)
            {
                //to do
            }

        }
        private async Task SaveConfig(MouseEventArgs e)
        {

            getSection["DefaultMMSqlOther"] = appSetting.connectionstrings.DefaultMMSqlOther;
            SaveConfigurationExt.SetAppSettingValue("DefaultMMSqlOther", appSetting.connectionstrings.DefaultMMSqlOther, "ConnectionStrings");

            var result = await OtherDBAppService.SetConnectionString(appSetting.connectionstrings.DefaultMMSqlOther);

            var msg = result.Status == 0 ? "保存成功,数据库连接OK" : $"数据库连接失败,{result.Message}";
            int delay = result.Status == 0 ? 3 : 10;
            await ToastService.Show(new ToastOption()
            {
                Category = result.Status == 0 ? ToastCategory.Success : ToastCategory.Error,
                Title = msg,
                Delay = delay * 1000,
                ForceDelay = true,
                Content = $"设置：{LocalizerConnect["DefaultMMSqlOther"]}:{msg}，{delay} 秒后自动关闭"
            });

            await Task.CompletedTask;
        }
        #endregion
    }

}
